// pages/onoff_manager/onoff_manager.js
var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    template_array:[]

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let header = {
      'content-type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    };

    wx.request({
      url: 'https://119.3.46.145/server/GetLightModal',
      method: 'POST',
      header: header,
      data: {
        userid: app.globalData.userid
      },
      success: res => {
        //data: { error: "0", info: "添加成功" }
        //console.log(res, "res");
        //{"data":"[{\"turnOff\":\"16:00:00\",\"turnOffChange\":\"20\",\"turnOn\":\"auto\",\"name\":\"测试1\",\"id\":\"2\",\"turnOnChange\":\"-10\"},{\"turnOff\":\"auto\",\"turnOffChange\":\"0\",\"turnOn\":\"auto\",\"name\":\"测试3\",\"id\":\"4\",\"turnOnChange\":\"0\"},{\"turnOff\":\"07:30:00\",\"turnOffChange\":\"10\",\"turnOn\":\"06:03:00\",\"name\":\"测试2-1\",\"id\":\"5\",\"turnOnChange\":\"-10\"}]"}
        let result = JSON.parse(res.data.data);

        that.setData({
          template_array: result
        });

        // let header = {
        //   'content-type': 'application/x-www-form-urlencoded',
        //   'cookie': "openid=" + app.globalData.openid
        // }
      },
      fail: function (error) {
        console.log(error, "GetLightModal error");
      }
    })
  },

  apply:function(e){
    let that = this;   
    let id = e.currentTarget.dataset.id;
    let templateName = e.currentTarget.dataset.name;   
    //console.log(templateName, 'templateName');

    wx.navigateTo({
      url: '../folder_list/folder_list?modalID=' + id + '&templateName=' + templateName + '&type=1'  //type=1表示要用到
    });
  },

//添加新模板
  add_template: function () {
    let that = this;
    wx.navigateTo({
      url: '../new_onoff_template/new_onoff_template',
    })
  },

//修改模板
  mod_template:function(e){
    let  that = this;      
    // {
    // id: "2"
    // name: "测试1"
    // turnOff: "16:00:00"
    // turnOffChange: "20"
    // turnOn: "auto"
    // turnOnChange: "-10" 
    // }
    let item = e.currentTarget.dataset.detail; 
    //console.log(item, "item");

    wx.navigateTo({
      url: '../edit_onoff_template/edit_onoff_template?id='+item.id +'&name='+item.name + '&turnOff='+item.turnOff+'&turnOffChange='+item.turnOffChange+'&turnOn='+item.turnOn+'&turnOnChange='+item.turnOnChange,
    });
  },

  //删除模板
  del_template:function(e){
    let that = this;
    //console.log(e, "del e")
    let id = e.currentTarget.dataset.id;

    let header = {
      'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
      'cookie': "openid=" + app.globalData.openid
    }


    wx.request({
      url: "https://119.3.46.145/server/deleteLightModal",
      type: "post",
      data: {
        id: id
      },
      success: function (res) {      
        //console.log(res)

        let error = res.data.error;
      
        if (error === "0") {
          wx.showModal({
            title: '提示',
            content: '删除成功',
            success: function () {
              wx.request({
                url: 'https://119.3.46.145/server/GetLightModal',
                header: header,
                data: {
                  userid: app.globalData.userid
                },
                success: function (res) {
                  // { "data": "[{\"turnOff\":\"16:00:00\",\"turnOffChange\":\"20\",\"turnOn\":\"auto\",\"name\":\"测试1\",\"id\":\"2\",\"turnOnChange\":\"-10\"},{\"turnOff\":\"auto\",\"turnOffChange\":\"0\",\"turnOn\":\"auto\",\"name\":\"测试3\",\"id\":\"4\",\"turnOnChange\":\"0\"},{\"turnOff\":\"07:30:00\",\"turnOffChange\":\"10\",\"turnOn\":\"06:03:00\",\"name\":\"测试2-1\",\"id\":\"5\",\"turnOnChange\":\"-10\"},{\"turnOff\":\"04:59\",\"turnOffChange\":\"10\",\"turnOn\":\"18:01\",\"name\":\"test5\",\"id\":\"7\",\"turnOnChange\":\"10\"},{\"turnOff\":\"23:59\",\"turnOffChange\":\"10\",\"turnOn\":\"00:01\",\"name\":\"test6\",\"id\":\"8\",\"turnOnChange\":\"10\"}]" }
                  let data = JSON.parse(res.data.data);

                  if (data.length == 0) {
                    return;
                  }

                  that.setData({
                    template_array: data
                  });
                
                },
                fail: function (error) {
                  console.log(error, "GetLightModal fail");
                }
              });
            }
          })
       
         
        }
        else if (error === "-1") {
          wx.showModal({
            title: '提示',
            content: '删除失败，该模式正在被使用',
          });
        }
        else {
          wx.showModal({
            title: '提示',
            content: '删除失败',
          });
        }
      },
      fail:function(){
        wx.showModal({
          title: '删除失败',
          content: '',
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }  
})